package com.wuxiaofan.search.controller;

import com.wuxiaofan.common.vo.ReturnResult;
import com.wuxiaofan.search.entity.User;
import java.util.List;
import javax.annotation.Resource;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zxp.esclientrhl.repository.ElasticsearchTemplate;

/**
 * @author wuxiaofan
 * @date 2021/7/26 5:37 下午
 */
@RestController
@RequestMapping("user")
public class  UserApi {
    @Resource
    ElasticsearchTemplate<User,String> elasticsearchTemplate;
    @Resource
    private RestHighLevelClient restHighLevelClient;

    @PutMapping("put")
    public ReturnResult put(@RequestBody User user) throws Exception {
        boolean update = elasticsearchTemplate.update(user);
        return ReturnResult.success("update",update);
    }

    @PostMapping("post")
    public ReturnResult post(@RequestBody User user) throws Exception {
        boolean save = elasticsearchTemplate.save(user);
        return ReturnResult.success("post",save);
    }

    /**
     * 保存数据
     * @return
     * @throws Exception
     */
    @RequestMapping("save")
    public ReturnResult save() throws Exception {
        User user = new User();
        user.setId("5");
        user.setName("王五");
        user.setDesc("测试的一个描述");
        user.setSelfIntroduction("自我简介");
        boolean save = elasticsearchTemplate.save(user);
        return ReturnResult.success("save",save);
    }

    @RequestMapping("del/{id}")
    public ReturnResult del(@PathVariable("id")String id) throws Exception {
        boolean save = elasticsearchTemplate.deleteById(id,User.class);
        return ReturnResult.success("save",save);
    }

    @RequestMapping("update")
    public ReturnResult update(@RequestBody User user) throws Exception {
        boolean update = elasticsearchTemplate.update(user);
        return ReturnResult.success("update",update);
    }


    @RequestMapping("list")
    public ReturnResult list() throws Exception {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");
        SearchResponse searchResponse = elasticsearchTemplate.search(searchRequest);
        return ReturnResult.success("searchResponse",searchResponse);
    }

    /**
     * 匹配
     * @return
     * @throws Exception
     */
    @RequestMapping("match")
    public ReturnResult match(String desc) throws Exception {
        QueryBuilder queryBuilder = new MatchQueryBuilder("desc",desc);
        List<User> userList = elasticsearchTemplate.search(queryBuilder,User.class);
        return ReturnResult.success("searchResponse",userList);
    }

    @RequestMapping("matchall")
    public ReturnResult matchall() throws Exception {
        QueryBuilder queryBuilder = new MatchAllQueryBuilder();
        List<User> userList = elasticsearchTemplate.search(queryBuilder,User.class);
        return ReturnResult.success("searchResponse",userList);
    }

    @RequestMapping("term")
    public ReturnResult term(String desc) throws Exception {
        QueryBuilder queryBuilder = new TermQueryBuilder("desc.keyword",desc);
        List<User> userList = elasticsearchTemplate.search(queryBuilder,User.class);
        return ReturnResult.success("searchResponse",userList);
    }

    /**
     * 短语匹配
     * @param desc
     * @param slop 相关度 默认为0
     * @return
     * @throws Exception
     */
    @RequestMapping("matchPhraseQuery")
    public ReturnResult matchPhraseQuery(String desc,int slop) throws Exception {
        QueryBuilder queryBuilder = new MatchPhraseQueryBuilder("desc",desc).slop(slop);
        List<User> userList = elasticsearchTemplate.search(queryBuilder,User.class);
        return ReturnResult.success("searchResponse",userList);
    }

    @RequestMapping("rangeQuery")
    public ReturnResult rangeQuery(int startId,int endId) throws Exception {
        QueryBuilder queryBuilder = new RangeQueryBuilder("id").from(startId).to(endId);
        List<User> userList = elasticsearchTemplate.search(queryBuilder,User.class);
        return ReturnResult.success("searchResponse",userList);
    }





}
